Skip to main content

Cloud Spanner OpenTelemetry Traces

Setup​

This sample requires Java and Maven.

  1. Follow the set-up instructions in the documentation.

  2. Enable APIs for your project.

    a. Click here to visit Cloud Platform Console and enable the Google Cloud Spanner API.

    b. Click here to visit Cloud Platform Console and enable the Cloud Trace API.

  3. Create a Cloud Spanner instance and database via the Cloud Plaform Console's Cloud Spanner section.

  4. Enable application default credentials by running the command gcloud auth application-default login.

Run the Example​

  1. Set up database configuration in the OpenTelemetryUsage.java class:

    String projectId = "my-project";
    String instanceId = "my-instance";
    String databaseId = "my-database";
  2. Configure trace data export. You can use either the OpenTelemetry [Collector](https://opentelemetry.io/docs/collector/quick-start/ with the OTLP Exporter or the Cloud Trace Exporter. By default, the Cloud Trace Exporter is used.

  • To use OTLP Exporter, Set up the OpenTelemetry Collector and update the OTLP endpoint in OpenTelemetryUsage.java class
    boolean useCloudTraceExporter = true; // Replace to false for OTLP
    String otlpEndpoint = "http://localhost:4317"; // Replace with your OTLP endpoint
  1. You can also enable API Tracing and SQL Statement Tracing by setting below options. Refer Traces for more details.

    SpannerOptions options = SpannerOptions.newBuilder()
    .setOpenTelemetry(openTelemetry)
    .setEnableExtendedTracing(true)
    .setEnableApiTracing(true)
    .build();
  2. Then run the application from command line, after switching to this directory:

    mvn exec:java -Dexec.mainClass="com.example.spanner.OpenTelemetryUsage"

You should start seeing traces in Cloud Trace .